<template>
  <div class="add-role">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span>|</span>
        <span>{{ id?'编辑角色':'添加角色' }}</span>
      </div>
      <div class="right">
        黑马程序员
      </div>
    </header>
    <main class="add-main">
      <div class="step-container">
        <!-- 步骤条：最少2个 el-step -->
        <!-- 黑色的是当前步骤，蓝色的是已完成 -->
        <!-- active 指定当前步骤的索引值 -->
        <el-steps direction="vertical" :active="nowActive">
          <el-step title="角色信息" />
          <el-step title="权限信息" />
          <el-step title="检查并完成" />
        </el-steps>
      </div>
      <div v-show="nowActive === 0" class="form-container">
        <div class="title">角色信息</div>
        <div class="form">
          <el-form ref="roleForm" class="form-box" :model="roleForm" :rules="roleRules">
            <el-form-item label="角色名称" prop="roleName">
              <el-input v-model="roleForm.roleName" />
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input v-model="roleForm.remark" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div v-show="nowActive === 1" class="form-container">
        <div class="title">权限配置</div>
        <div class="form">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title">{{ item.title }}</div>
              <el-tree
                ref="tree"
                :data="item.children"
                show-checkbox
                default-expand-all
                check-strictly
                node-key="id"
                highlight-current
                :props="{ label: 'title' }"
              />
            </div>
          </div>
        </div>
      </div>
      <div v-show="nowActive === 2" class="form-container">
        <div class="title">检查并完成</div>
        <div class="form">
          <div class="info">
            <div class="form-item">角色名称：{{ roleForm.roleName }}</div>
            <div class="form-item">角色描述：{{ roleForm.remark }}</div>
            <div class="form-item">角色权限：</div>
            <div class="tree-wrapper">
              <div v-for="item in treeList" :key="item.id" class="tree-item">
                <div class="tree-title">{{ item.title }}</div>
                <el-tree
                  ref="diabledTree"
                  :data="item.children"
                  show-checkbox
                  default-expand-all
                  check-strictly
                  node-key="id"
                  :highlight-current="false"
                  :props="{ label: 'title',disabled:()=>true}"
                />
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="last">上一步</el-button>
        <el-button type="primary" @click="next">
          {{ nowActive === 2 ? id?'确定修改':'确定添加' : '下一步' }}
        </el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { getTreeListAPI, createRoleUserAPI, getRoleDetailAPI, updateRoleAPI } from '@/apis/system'
export default {
  data() {
    return {
      nowActive: 0,
      roleForm: {
        roleName: '',
        remark: ''
      },
      roleRules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
      },
      treeList: [], // 树形结构列表
      perms: [],
      detailPerms: []
    }
  },
  computed: {
    id() {
      return this.$route.query.roleId
    }
  },
  created() {
    this.getTreeList()
    if (this.id) {
      this.roleDetailFn()
    }
  },
  methods: {
    // 上一步
    last() {
      if (this.nowActive === 2 && this.id) {
        this.$refs.tree.forEach((item, index) => {
          item.setCheckedKeys(this.perms[index])
        })
      }
      this.nowActive > 0 && this.nowActive--
    },
    // 下一步
    async next() {
      if (this.nowActive === 0) {
        await this.$refs.roleForm.validate()
        this.nowActive < 2 && this.nowActive++
        // 第二步：权限点回显
        if (this.id) {
          this.$refs.tree.forEach((item, index) => {
            item.setCheckedKeys(this.detailPerms[index])
          })
        }
      } else if (this.nowActive === 1) {
        this.perms = []
        // 分配权限点
        this.$refs.tree.forEach((treeCom) => {
          // 把树组件选中的行的id（node-key绑定的字段名）收集起来
          const keys = treeCom.getCheckedKeys()
          this.perms.push(keys)
        })
        // 数组扁平化
        const resultArr = this.perms.flat()
        if (resultArr.length > 0) {
          this.nowActive < 2 && this.nowActive++
          this.$message.success('分配成功')
          this.$nextTick(() => {
            // 把收集到的id赋值给树组件
            // 注意：这里要使用refs，因为树组件是动态渲染的
            // 注意：这里要使用forEach，因为树组件是动态渲染的
            // 注意：这里要使用setCheckedKeys，因为我们要设置选中的节点
            // 注意：这里要使用$nextTick，因为要等DOM更新后再执行
            this.$refs.diabledTree.forEach((treeCom, index) => {
              treeCom.setCheckedKeys(this.perms[index])
            })
          })
        } else {
          this.$message.error('请分配权限点')
          return
        }
      } else {
        // 提交表单
        if (this.id) {
          await updateRoleAPI({
            ...this.roleForm,
            perms: this.perms,
            roleId: this.id
          })
          this.$message.success('编辑成功')
        } else {
          await createRoleUserAPI({
            ...this.roleForm,
            perms: this.perms
          })
          this.$message.success('添加成功')
        }

        setTimeout(() => {
          this.$router.back()
        }, 1500)
      }
    },
    async getTreeList() {
      const res = await getTreeListAPI()
      this.treeList = res.data
    },
    // 请求指定角色下属的功能权限列表集合
    async roleDetailFn() {
      const roleRes = await getRoleDetailAPI(this.id)
      console.log(roleRes)
      // 重要：把模版里绑定的对象重新赋予了一个新的对象，响应式丢失
      // 结论：置空/回显，尽量去修改原对象里的属性，不要赋予新对象
      this.roleForm.remark = roleRes.data.remark
      this.roleForm.roleName = roleRes.data.roleName
      this.detailPerms = roleRes.data.perms
    }
  }

}
</script>
<style scoped lang="scss">
.add-role {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    position: fixed;
    top: 64px;
    bottom: 88px;
    width: 100%;
    overflow-y: auto;
    background: #f4f6f8;
    padding: 20px 230px;
    display: flex;
    .step-container {
      height: 400px;
      width: 200px;
    }
    .form-container {
      flex:1;
      background-color: #fff;
      overflow-y: auto;
      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }
      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;
        .el-form {
          display: flex;
          flex-wrap: wrap;
        }
        .info{
          font-size:14px;
          color:#666;
          .form-item{
            margin-bottom:20px;
          }
        }
      }
      .form-box{
        width: 600px;
        display: flex;
        flex-direction: column;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
    z-index: 10001;
  }
  .tree-wrapper {
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      .tree-item {
        flex: 1;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }
}
</style>
